package com.config;

import com.netty.client.SystemHandler;
import com.netty.client.WebSocketClient;

import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@EnableScheduling
@Slf4j
public class SystemListener {
    public WebSocketClient webSocketClient;

    @Autowired protected Environment environment;



    public Logger logger = LogManager.getLogger("【websocket.client】"+getClass().getName());

    boolean first=false;

    @Scheduled(cron = "0/5 * * * * ?")    //每5秒执行一次
    public void run() {
        if (first) { return; }

        String url = environment.getProperty("websocket.urlSystem");
        if (webSocketClient == null && url !=null && url.length() > 2) {
            webSocketClient = new WebSocketClient(SystemHandler.class, "websocket.urlSystem");
            webSocketClient.setReConnectInterval(4);//设置，如果网络断开，重新连接的间隔时间
            new Thread(webSocketClient).start();
            //logger.debug("已开启 WebSocket 系统队列");
        }

//        //luojp add 接受在线用户消息,开发库 测试库 航运用  接收到更新 当前数据库中的 用户状态
//        url = JdbcProperties.getString("websocket.urlOnline");
//        if (url !=null && url.length() > 2) {
//            new Thread(new WebSocketClient(OnlineUserHandler.class, "websocket.urlOnline")).start();
//            logger.debug("已开启 WebSocket 在线用户队列");
//        }
//        first=true;
    }
}